package com.czy.owner.adapter;


import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.czy.owner.R;
import com.czy.owner.db.UserHelper;
import com.czy.owner.entity.GetCouponsCenterModel;
import com.czy.owner.global.Constants;
import com.czy.owner.net.utils.JsonUtil;
import com.czy.owner.rxbus.RxBus;
import com.czy.owner.rxbus.event.CommonEvent;
import com.czy.owner.ui.hotshop.ShoppingCartActivity;
import com.czy.owner.ui.login.LoginActivity;
import com.czy.owner.utils.MyLog;
import com.czy.owner.utils.PhoneUtils;
import com.czy.owner.utils.SharedPreferencesUtils;
import com.czy.owner.utils.TimeUtils;
import com.easyrecycleview.adapter.BaseViewHolder;
import com.easyrecycleview.adapter.RecyclerArrayAdapter;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by wangyang on 2017/4/14 0014.
 */

public class GetCouponsCenterAdapter extends RecyclerArrayAdapter<GetCouponsCenterModel.ListBean> {
    private Context ctx;
    private OnGetCouponsListener onGetCoupons;

    public GetCouponsCenterAdapter(Context context) {
        super(context);
        this.ctx = context;
    }

    @Override
    public BaseViewHolder OnCreateViewHolder(ViewGroup parent, int viewType) {
        return new CouponsCenterViewHolder(parent, ctx);
    }

    class CouponsCenterViewHolder extends BaseViewHolder<GetCouponsCenterModel.ListBean> {
        private int yellowColor = Color.parseColor("#FDCD54");
        private int themeColor = Color.parseColor("#09c0c7");

        private LinearLayout ll_bg;
        private TextView tv_use_store;
        private LinearLayout ll_xuxian;
        private TextView tv_conpons_type;
        private ImageView img_coupons_type;
        private TextView tv_used_case;
        private TextView tv_used_data;
        private TextView tv_used_time;
        private TextView tv_used_money;
        private TextView tv_used_yuan;
        private TextView tv_get_conpons;
        private TextView tv_description;
        private Context ctx;


        public CouponsCenterViewHolder(ViewGroup parent, Context ctx) {
            super(parent, R.layout.item_get_coupons_center);
            ll_bg = $(R.id.ll_bg);
            tv_use_store = $(R.id.tv_use_store);
            ll_xuxian = $(R.id.ll_xuxian);
            tv_conpons_type = $(R.id.tv_conpons_type);
            img_coupons_type = $(R.id.img_coupons_type);
            tv_used_case = $(R.id.tv_used_case);
            tv_used_data = $(R.id.tv_used_data);
            tv_used_time = $(R.id.tv_used_time);
            tv_used_money = $(R.id.tv_used_money);
            tv_used_yuan = $(R.id.tv_used_yuan);
            tv_get_conpons = $(R.id.tv_get_conpons);
            tv_description = $(R.id.tv_description);
            this.ctx = ctx;
        }

        @Override
        public void setData(final GetCouponsCenterModel.ListBean model) {
            if (model.getUseType().equals("only")) {
                img_coupons_type.setImageResource(R.mipmap.bg_coupons_unusered_red);
            } else if (model.getUseType().equals("all")) {
                img_coupons_type.setImageResource(R.mipmap.bg_coupons_common_yellow);
            } else {
                img_coupons_type.setVisibility(View.GONE);
            }
            String type = model.getBelongsType();

            if (type.equals("store")) { //黄色门店
                ll_bg.setBackgroundResource(R.mipmap.coupons_red_bg);
                ll_xuxian.setBackgroundResource(R.mipmap.coupons_xuxian_red_bg);
                tv_use_store.setBackgroundResource(R.drawable.selector_user_store_bg);
                tv_use_store.setTextColor(yellowColor);
                tv_use_store.setText("限" + UserHelper.getInstance().getOwnerInfoModel(ctx).getStoreName() + "使用");
                tv_conpons_type.setTextColor(yellowColor);
                tv_used_data.setTextColor(yellowColor);
                tv_used_time.setTextColor(yellowColor);
                tv_used_money.setTextColor(yellowColor);
                tv_used_yuan.setTextColor(yellowColor);
            } else if(type.equals("system")){ // 0 系统
                ll_bg.setBackgroundResource(R.mipmap.coupons_blue_bg);
                ll_xuxian.setBackgroundResource(R.mipmap.coupons_xuxian_blue_bg);
                tv_use_store.setBackgroundResource(R.drawable.selector_user_store_system_coupons_bg);
                tv_use_store.setTextColor(themeColor);
                tv_use_store.setText("限平台商城使用");
                tv_use_store.setVisibility(View.VISIBLE);
                tv_conpons_type.setTextColor(themeColor);
                tv_used_data.setTextColor(themeColor);
                tv_used_time.setTextColor(themeColor);
                tv_used_money.setTextColor(themeColor);
                tv_used_yuan.setTextColor(themeColor);
            }else if(type.equals("transfer")){
                ll_bg.setBackgroundResource(R.mipmap.coupons_blue_bg);
                ll_xuxian.setBackgroundResource(R.mipmap.coupons_xuxian_blue_bg);
                tv_use_store.setBackgroundResource(R.drawable.selector_user_store_system_coupons_bg);
                tv_use_store.setTextColor(themeColor);
                tv_use_store.setText("限平台商城使用");
                tv_conpons_type.setTextColor(themeColor);
                tv_used_data.setTextColor(themeColor);
                tv_used_time.setTextColor(themeColor);
                tv_used_money.setTextColor(themeColor);
                tv_used_yuan.setTextColor(themeColor);

            }

            if(model.getUseGoodsType().equals("transfer")){
                tv_use_store.setVisibility(View.GONE);
            }else {
                tv_use_store.setVisibility(View.VISIBLE);
            }

            tv_conpons_type.setText(model.getCouponsGroupName());
            tv_description.setText(model.getDescription());
            if (model.getMinimum() == 0) {
                tv_used_case.setText("无门槛");
            } else {
                tv_used_case.setText("满" + String.format("%.2f", model.getMinimum()) + "元使用");
            }
            int validityType = model.getValidityType();
            int fixTimeValue = model.getFixTimeValue();
            long startTime = model.getStartTime();
            long endTime = model.getEndTime();
            if (validityType == 0) {
                tv_used_time.setText("永久有效");
            } else if (validityType == 1) {
                tv_used_time.setText(fixTimeValue + "天有效");
            } else if (validityType == 2) {
                tv_used_time.setText(TimeUtils.formatTimeDifference4(startTime) + "-" + TimeUtils.formatTimeDifference4(endTime));
            } else {
                tv_used_time.setText("永久有效");
            }
            tv_used_money.setText(String.format("%.2f", model.getDenomination()));
            //tv_used_time.getPaint().setFlags(Paint. STRIKE_THRU_TEXT_FLAG|Paint.ANTI_ALIAS_FLAG);  // 设置中划线并加清晰
            tv_get_conpons.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onGetCoupons != null) {
                        onGetCoupons.getCouponsOnClick(model, getDataPosition());
                    }
                }
            });
        }
    }


    public interface OnGetCouponsListener {
        void getCouponsOnClick(GetCouponsCenterModel.ListBean model, int position);
    }

    public void setCouponsOnClick(OnGetCouponsListener onGetCoupons) {
        this.onGetCoupons = onGetCoupons;
    }
}